#!/usr/bin/ksh

#########################################################################
#									#
# Name:		mkpkg.rpm (Shell script)				#
#									#
# Completed:	28th August, 2003.					#
#									#
# Updated:	27th September, 2005.					#
#									#
# Author:	Simon Edwards, Proprius Consulting Ltd.			#
#									#
# Arguments:	-V		Print codemgr packaging type - 		#
#				currently set to 1.			#
#									#
# Arguments:	-r rel [-x cfg] [-v] [-f]				#
#		-r		Indicate the number of the release to	#
#				create - assigned by the 		#
#				mkpkg wrapper script usually.		#
#		-x		Specify an alternative cfg file - 	#
#				the default will be "cfg".		#
#		-f		Force create - if a version of this	#
#				release already exists it should be	#
#				over-written.				#
#									#
# Purpose:	This creates a RPM package based on the files in	#
#		the specified source directories. The toplevel mkpkg	#
#		will have checked that all files are checked in, and	#
#		give the relevant version information for the 		#
#		release.						#
#									#
# Limitations:	1 - No use of the more complex features of RPM's are	#
#		    included. This will create "binary-only" packages.	#
#									#
# Environment:	RELMGR_CFG	- Defines the file name containing the	#
#				  configuration settings for the 	#
#				  release manager for this user.	#
#									#
# Exit Codes:	0  - Package created successfully / all files in 	#
#		     version control system.				#
#		>1 - An error has occurred / not all files under 	#
#		     version control are checked back in.		#
# 									#
# Version:	@(#)1.1 Original (SE)>					#
# 		@(#)1.2 Support for per-type build depots (SE)>		#
#									#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#									#
# Complete RCS Version History						#
#									#
# Version:	1.2							#
# Date:		29th September, 2003.					#
# Author:	Simon Edwards, Proprius Consulting Ltd.			#
# Change:	Support per-type build depot, in this instance it is	#
#		DEPOT.RPM.						#
# Version:								#
# Date:									#
# Author:								#
# Change:								#
#									#
#########################################################################

function logmsg
{
	[[ $ARG_verbose -eq 0 ]] && return
	print "MSG  : $1"
}

function logwarn
{
	print "WARN : $1"
}

function logwarnfile
{
	while read cline
	do
		print "WARN : $cline"
	done < $1
}

function logerr
{
	print -u2 "ERROR: $1"
	[[ -n "$2" ]] && exit $2
}

function logerrfile
{
	while read cline
	do
		print "ERROR: $cline"
	done < $1
	[[ -n "$2" ]] && exit $2
}

SCRIPT=$(basename $0)
function loadvar 
{
	#################################################################
	# If we do not currently have a pkg directory, then create one!	#
	#################################################################

	[[ ! -d $1/pkg ]] && {
		mkdir -p $1/pkg 2>/dev/null || {
			logerr "Unable to create pkg directory" 2;
		}
		logwarn "Pkg directory auto-created!";
	}

	[[ ! -f $1/pkg/$CFGFILE ]] && {
		logerr "Configuration file \"$1/pkg/$CFGFILE\" does not exist." 5;
	}
	if [[ -n "$3" && "$3" = m ]]
	then
		( . $1/pkg/$CFGFILE 2>/dev/null; eval print \$$2 )
	elif [[ -n "$3" && "$3" = s ]]
	then
		awk -v searchfor="$2" '$1 == "#" {next} $1 ~ searchfor {split($0,a,"="); if(substr(a[1],1,length(searchfor)) == searchfor) { printf("%s %s\n",a[1],a[2]);exit(0);}}' \
		$1/pkg/$CFGFILE
	else
		awk -v searchfor="$2" '$1 == "#" {next} $1 ~ searchfor {split($0,a,"="); if(substr(a[1],1,length(searchfor)) == searchfor) { printf("%s %s\n",a[1],a[2]);}}' \
		$1/pkg/$CFGFILE
	fi
	return 0
}

function usage
{
	print -u2 "Usage: $SCRIPT -r release [-f] [-v]

Written by Simon Edwards 2000 - 2003, proprius.co.uk.
Please see the codemgr_$SCRIPT(1) manual page for more details.
"
exit 1
}

[[ -z "$RELMGR_CFG" ]] && {
	logerr "Variable \"RELMGR_CFG\" is not defined." 10;
}

[[ ! -f "$RELMGR_CFG" ]] && {
	logerr "\"RELMGR_CFG\" setting ($RELMGR_CFG) is not valid." 11;
}


whoami=$(whoami)

#########################################################################
# Now source the configuration information...				#
#########################################################################

. $RELMGR_CFG

export PROJECT_PREFIX PROJECT_PKGDIR PROJECT_RELDB VCTYPE
export DEFAULT_GROUP DEFAULT_PERMS DEFAULT_OWNER IGNORE_SUFFIXES
export FINAL_DEPOT BUILD_DEPOT

#########################################################################
# If the wrapper has been to generate the package it will have set the	#
# BUILD_IN_TMP variable, which now becomes the PROJECT_PREFIX for this	#
# particular package build.						#
#########################################################################

if [[ -n "$BUILD_IN_TMP" && -d $BUILD_IN_TMP ]]
then
	logmsg "PROJECT_PREFIX modified to $BUILD_IN_TMP"
	PROJECT_PREFIX=$BUILD_IN_TMP
fi

#########################################################################
# Now we have the basic details we need to parse the command line	#
# arguments to ensure everything looks ok.				#
#########################################################################

VERSION=0
FORCE=0
RELEASE=""
ARG_verbose=0
CFGFILE=""

while getopts ":Vfvr:x:" argval
do
	case $argval in
		V)	VERSION=1		;;
		f)	FORCE=1			;;
		v)	ARG_verbose=1		;;
		x)	CFGFILE=$OPTARG		;;
		r)	RELEASE=$OPTARG		;;
		*)	usage
	esac
done

if [[ $VERSION = 1 ]]
then
	print 1
	exit 0
fi

[[ -z "$CFGFILE" ]] && CFGFILE=cfg
[[ -z "$RELEASE" ]] && usage

#########################################################################
# We now work out the current product name, using the current directory	#
# path as a starting point. If the current directory path is not	#
# path of the PROJECT_PREFIX then we need to raise an error.		#
#########################################################################

project_name=$(print $PWD|
	awk -v projs=$PROJECT_PREFIX '{if(substr($1,1,length(projs))!=projs) exit(0);
		split(substr($1,2+length(projs)),a,"/");
		print a[1];}')

if [[ -z "$project_name" ]]
then
	logerr "Current path is invalid for a project!"
	logerr "Path must be sub-directory of \"$PROJECT_PREFIX\"." 12
fi

logmsg "Project name ascertained to be: $project_name"

rpmrcfile=$PROJECT_PREFIX/$project_name/pkg/.rpmrc
print "RPM_BUILD_DIR=$PROJECT_PREFIX/$project_name" >$rpmrcfile || {
	logerr "Unable to write to RPM rc file - aborting";
	logerr "File is $rpmrcfile" 13;
}


#########################################################################
# Before we do anything make sure we are able to load the minimum	#
# number of details to define the project package.			#
#########################################################################

logmsg "Loading required package settings..."

pkg_name=$(loadvar $PROJECT_PREFIX/$project_name NAME s|awk '{print $2}')
pkg_depot=$(loadvar $PROJECT_PREFIX/$project_name DEPOT.RPM s|awk '{print $2}')
[[ -z "$pkg_depot" ]] &&
	pkg_depot=$(loadvar $PROJECT_PREFIX/$project_name DEPOT s|awk '{print $2}')
pkg_desc=$(loadvar $PROJECT_PREFIX/$project_name DESCRIPTION s|awk '{for(n=2; n<=NF; n++) printf("%s ",$n);}')
pkg_ldesc=$(loadvar $PROJECT_PREFIX/$project_name DESCRIPTION m|awk '{for(n=2; n<=NF; n++) printf("%s ",$n);}')
pkg_license=$(loadvar $PROJECT_PREFIX/$project_name LICENSE s|awk '{for(n=2; n<=NF; n++) printf("%s ",$n);}')
pkg_ignore_suffixes=$(loadvar $PROJECT_PREFIX/$project_name IGNORE_SUFFIXES s|awk '{print $2}' | sed 's/"//g')
pkg_auto_dependencies=$(loadvar $PROJECT_PREFIX/$project_name AUTO_DEPENDENCIES s|awk '{print $2}' | sed 's/"//g')
pkg_arch=$(loadvar $PROJECT_PREFIX/$project_name ARCHITECTURE s|awk '{print $2}')
pkg_vendor=$(loadvar $PROJECT_PREFIX/$project_name VENDOR s|awk '{print $2}')
[[ -z "pkg_vendor" ]] && 
	pkg_vendor=$(loadvar $PROJECT_PREFIX/$project_name VENDOR1 s|awk '{print $2}')

#########################################################################
# If no package architecture, then use the uname -p as the 		#
# architecture type.							#
#########################################################################

if [[ -z "$pkg_arch" ]]
then
	pkg_arch="$(uname -p)"
	logwarn "Package architecture defaulted to $pkg_arch"
fi

if [[ "$pkg_arch" = GENERIC ]]
then
	pkg_arch=noarch
fi

#########################################################################
# If no vendor is specified then default it to "NA".			#
#########################################################################

if [[ -z "$pkg_vendor" ]]
then
	pkg_vendor=NA
	logwarn "Package vendor defaulted to \"$pkg_vendor\""
fi

#########################################################################
# If the auto_dependencies is not set, then it is set to "off" by 	#
# default.								#
#########################################################################

if [[ -z "$pkg_auto_dependencies" ]] 
then
	pkg_auto_dependencies="$IGNORE_DEPENDENCIES"
fi
if [[ -z "$pkg_auto_dependencies" ]]
then
	pkg_auto_dependencies="off"
	logmsg "Default package autodependencies to \"off\" for RPM build."
fi

if [[ "$pkg_auto_dependencies" != "on" && "$pkg_auto_dependencies" != "off" ]]
then
	logwarn "Setting \"$pkg_auto_dependencies\" is invalid, defaulting to \"off\"."
	pkg_auto_dependencies="off"
fi

if [[ -z "$pkg_ignore_suffixes" && -n "$IGNORE_SUFFIXES" ]]
then
	pkg_ignore_suffixes="$IGNORE_SUFFIXES"
	logmsg "Ignored suffixes for packaging: $pkg_ignore_suffixes"
fi

pkg_build=$(loadvar $PROJECT_PREFIX/$project_name BUILD s|awk '{print $2}')
pkg_buildscript=$(loadvar $PROJECT_PREFIX/$project_name BUILDSCRIPT s|awk '{print $2}')

[[ -z "$pkg_name" ]] &&
	pkg_name="$project_name" &&
	logwarn "Defaulting package name to $pkg_name"

[[ -z "$pkg_depot" ]] &&
	pkg_depot="$HOME/rpm" &&
	logwarn "Defaulting package depot to $pkg_depot"

[[ -z "$pkg_build" ]] &&
	pkg_build="$HOME/rpm-build" &&
	logwarn "Defaulting package build directory to $pkg_build"

[[ -z "$pkg_desc" ]] &&
	pkg_desc="Default short description for $pkg_name" &&
	logwarn "Defaulting short package description"

[[ -z "$pkg_ldesc" ]] &&
	pkg_ldesc="Default long description for $pkg_name" &&
	logwarn "Defaulting long package description"

[[ -z "$pkg_license" ]] &&
	pkg_license="GPL" &&
	logwarn "Defaulting license to $pkg_license"

[[ ! -d "$pkg_depot" ]] && {
	mkdir -p $pkg_depot 2>/dev/null || {
		logerr "Unable to create package depot $pkg_depot" 14;
	} ; 
	logmsg "Created Build directory: $pkg_depot";
}

[[ ! -d "$pkg_build" ]] && {
	mkdir -p $pkg_build 2>/dev/null || {
		logerr "Unable to create package build directory $pkg_build" 15;
	} ; 
	logmsg "Created Build Root directory: $pkg_build";
}

#########################################################################
# Optional single word configuration variables.				#
#########################################################################

logmsg "Loading optional configuration parameters..."

pkg_cat=$(loadvar $PROJECT_PREFIX/$project_name CATEGORY|awk '{print $2}')
pkg_osrelease=$(loadvar $PROJECT_PREFIX/$project_name OSRELEASE|awk '{print $2}')
pkg_topdir=$(loadvar $PROJECT_PREFIX/$project_name TOPDIR|awk '{print $2}')
pkg_preinstall=$(loadvar $PROJECT_PREFIX/$project_name PREINSTALL|awk '{print $2}')
pkg_postinstall=$(loadvar $PROJECT_PREFIX/$project_name POSTINSTALL|awk '{print $2}')
pkg_checkinstall=$(loadvar $PROJECT_PREFIX/$project_name CHECKINSTALL|awk '{print $2}')
pkg_preremove=$(loadvar $PROJECT_PREFIX/$project_name PREREMOVE|awk '{print $2}')
pkg_postremove=$(loadvar $PROJECT_PREFIX/$project_name POSTREMOVE|awk '{print $2}')
pkg_dependencies=$(loadvar $PROJECT_PREFIX/$project_name DEPENDS_ON|awk '{print $2}')


#########################################################################
# Now the actual software packaging - at last!				#
#########################################################################

psf_file="$PROJECT_PREFIX/$project_name/pkg/$pkg_name.spec"

[[ ! -d $pkg_build/$pkg_name ]] && mkdir $pkg_build/$pkg_name 
logmsg "Building RPM package SPEC file..."
exec 3>$psf_file
print -u3 "Summary: ${pkg_desc:-Summary for package $pkg_name}"
print -u3 "Name: $pkg_name"
print -u3 "Version: $RELEASE"
print -u3 "Release: 1"
print -u3 "License: $pkg_license"
print -u3 "Group: ${pkg_cat:-System Administration}"
[[ "$pkg_auto_dependencies" = "off" ]] && print -u3 "AutoReqProv: no"
print -u3 "BuildRoot: $pkg_build/%{name}"
print -u3 ""
print -u3 "%description"
print -u3 "$pkg_ldesc"

# BUILD
print -u3 ""
print -u3 "%install"
print -u3 "rm -rf \$RPM_BUILD_ROOT"
print -u3 "mkdir -p \$RPM_BUILD_ROOT"

#########################################################################
# Now create the directories under the build root to copy
# the information into...						#
#########################################################################

ignore_file="/tmp/pkg.$$.ignorelist"
logmsg "Building package install list..."
predir=""
predestdir=""
loadvar $PROJECT_PREFIX/$project_name DIR: |
while read dir dest
do
	integer ctfiles=0
        dir=$(print $dir|awk -v FS=: '{print $2}')
	[[ -f  $PROJECT_PREFIX/$project_name/$dir/.ignore ]] && {
		logmsg "Ignoring directory $dir";
		continue;
	}
	mydir=$(print "$dest" | awk -v FS=, '{print $1}')
	print -u3 "mkdir -p \$RPM_BUILD_ROOT/$mydir"

	#################################################################
	# Get the user, group and permissions, if specified		#
	#################################################################

	user=$(print $dest|awk -v FS=, '{print $2}')
	user=${user:-root}
	group=$(print $dest|awk -v FS=, '{print $3}')
	group=${group:-root}
	perms=$(print $dest|awk -v FS=, '{print $4}')
	perms=${perms:-750}
	destdir=$(print $dest|awk -v FS=, '{print $1}')

	#################################################################
	# Now check if extended DIR:ddd(file)... syntax has been	#
	# used...							#
	#################################################################

	pickfiles=$(print $dir|
		awk '{s1=index($0,"("); s2=index($0,")");
			if(s1>0 && s2>0) {
				print substr($0,s1+1,s2-s1-1);
			}
		     }')

	[[ -z "$pickfiles" ]] && pattern="*"
	[[ -n "$pickfiles" ]] && pattern=$(print "$pickfiles" | tr ',' ' ')

	mycd=$PWD

	#################################################################
	# Chop of the filtered files bit from the directory name	#
	# if required.							#
	#################################################################

	[[ -n "$pickfiles" ]] && {
		dir=$(print $dir|
			awk '{s1=index($0,"("); print substr($0,1,s1-1);}');
	}

	cd $PROJECT_PREFIX/$project_name/$dir 2>/dev/null || {
		logwarn "Directory \"$dir\" does not exist - ignoring.";
		continue;
	}

	#################################################################
	# Because of the extended attribute the "DIR:" might be used	#
	# multiple times for the same directory - if so, only repear	#
	# the "directory" line if similar lines are grouped.		#
	#################################################################

	predir=$dir
	predestdir=$destdir

	#################################################################
	# Now dump all the files in this directory...			#
	#################################################################

	for cfile in $(eval ls -d "$pattern" 2>/dev/null)
	do
		[[ ! -f $cfile ]] && continue
		#########################################################
		# Ignore "ignore files...				#
		#########################################################

		myext="$(print $cfile|awk -v FS="." 'NF>1 {print $NF}')"
		[[ "$myext" = "ignore" ]] && continue

		if [[ -f "$cfile.ignore" ]]
		then
			continue
		fi
		
		#################################################
		# Here we check to see if the suffix matches	#
		# one of those in the pkg_ignore_suffixes list.	#
		#################################################

		if [[ -n "$pkg_ignore_suffixes" ]]
		then
			myignore=0
			mysuff=".${cfile##*.}"
			for suffix in $pkg_ignore_suffixes
			do
				if [[ "$suffix" = "$mysuff" ]]
				then
					myignore=1
					break
				fi
			done
			if [[ $myignore -eq 1 ]]
			then
				logmsg "Excluding \"$dir/$cfile\" from package build."
				print "rm -f \$RPM_BUILD_ROOT/$destdir/$cfile" >>$ignore_file
				continue
			fi
		fi
		if [[ $whoami = root ]]
		then
			print -u3 "install -o $user -g $group -m $perms $PWD/$cfile \$RPM_BUILD_ROOT/$destdir/$cfile"
		else
			print -u3 "install -m $perms $PWD/$cfile \$RPM_BUILD_ROOT/$destdir/$cfile"
		fi
	done
	cd $mycd
done

print -u3 "\n%clean\nrm -rf \$RPM_BUILD_ROOT"

# If we have a build section then put it in here...			'

if [[ -n "$pkg_buildscript" || -s $igore_file ]]
then
	print -u3 ""
	print -u3 "%build"
	if [[ -n "$pkg_buildscript" ]]
	then
		print -u3 "cd $PROJECT_PREFIX/$project_name/$(dirname $pkg_buildscript)"
		print -u3 "$(cat $PROJECT_PREFIX/$project_name/$pkg_buildscript)"
	fi
	if [[ -s $ignore_file ]]
	then
		cat $ignore_file >&3
	fi
	print -u3 ""
fi


[[ -f $ignore_file ]] && rm -f $ignore_file
print -u3 "\n%files"
print -u3 "%defattr(${DEFAULT_PERMS:-555},${DEFAULT_OWNER:-root},${DEFAULT_GROUP:-root})"

#########################################################################
# Now we need to list all the files that the package will create	#
#########################################################################

logmsg "Adding software to package build..."
integer tfiles=0
integer tdirectories=0
predir=""
predestdir=""
loadvar $PROJECT_PREFIX/$project_name DIR: |
while read dir dest
do
        dir=$(print $dir|awk -v FS=: '{print $2}')
	[[ -f  $PROJECT_PREFIX/$project_name/$dir/.ignore ]] && {
		print -u2 "INFO: Ignoring directory $dir";
		continue;
	}

	#################################################################
	# Get the user, group and permissions, if specified		#
	#################################################################

	user=$(print $dest|awk -v FS=, '{print $2}')
	[[ -z "$user" ]] &&
		user=${DEFAULT_OWNER:-root}
	group=$(print $dest|awk -v FS=, '{print $3}')
	[[ -z "$group" ]] &&
		group=${DEFAULT_GROUP:-root}
	perms=$(print $dest|awk -v FS=, '{print $4}')
	[[ -z "$perms" ]] &&
		perms=${DEFAULT_PERMS:-555}
	destdir=$(print $dest|awk -v FS=, '{print $1}')

	#################################################################
	# Now check if extended DIR:ddd(file)... syntax has been	#
	# used...							#
	#################################################################

	pickfiles=$(print $dir|
		awk '{s1=index($0,"("); s2=index($0,")");
			if(s1>0 && s2>0) {
				print substr($0,s1+1,s2-s1-1);
			}
		     }')

	[[ -z "$pickfiles" ]] && pattern="*"
	[[ -n "$pickfiles" ]] && pattern=$(print "$pickfiles" | tr ',' ' ')

	logmsg "Packaging directory $dir [destination $destdir]"
	mycd=$PWD

	#################################################################
	# Chop of the filtered files bit from the directory name	#
	# if required.							#
	#################################################################

	[[ -n "$pickfiles" ]] && {
		dir=$(print $dir|
			awk '{s1=index($0,"("); print substr($0,1,s1-1);}');
	}

	cd $PROJECT_PREFIX/$project_name/$dir 2>/dev/null || {
		logwarn "Directory \"$dir\" does not exist - ignoring.";
		continue;
	}

	#################################################################
	# Because of the extended attribute the "DIR:" might be used	#
	# multiple times for the same directory - if so, only repear	#
	# the "directory" line if similar lines are grouped.		#
	#################################################################

	predir=$dir
	predestdir=$destdir

	#################################################################
	# Now dump all the files in this directory...			#
	#################################################################

	integer tsizes=0
	for cfile in $(eval ls -d "$pattern" 2>/dev/null)
	do
		[[ ! -f "$cfile" ]] && continue
		#########################################################
		# Ignore "ignore files...				#
		#########################################################

		myext="$(print $cfile|awk -v FS="." 'NF>1 {print $NF}')"
		[[ "$myext" = "ignore" ]] && continue

		if [[ -f "$cfile.ignore" ]]
		then
			logmsg "Excluding \"$dir/$cfile\" from package."
			continue
		else
			#################################################
			# Here we check to see if the suffix matches	#
			# one of those in the pkg_ignore_suffixes list.	#
			#################################################

			if [[ -n "$pkg_ignore_suffixes" ]]
			then
				myignore=0
				mysuff=".${cfile##*.}"
				[[ "$mysuff" = ".$cfile" ]] && {
					tsizes=$(($tsizes+$(du -sk $cfile|awk '{print $1}') ));
					let ctfiles=ctfiles+1;
					print -u3 "$predestdir/$cfile\n%attr($perms, $user, $group) $predestdir/$cfile";
					continue;
				}

				for suffix in $pkg_ignore_suffixes
				do
					if [[ "$suffix" = "$mysuff" ]]
					then
						myignore=1
						break
					fi
				done
				if [[ $myignore -eq 1 ]]
				then
					logmsg "Excluding \"$dir/$cfile\" from package distribution."
					continue
				fi
			fi
		fi
		
		tsizes=$(($tsizes+$(du -sk $cfile|awk '{print $1}') ))
		let ctfiles=ctfiles+1
		print -u3 "$predestdir/$cfile\n%attr($perms, $user, $group) $predestdir/$cfile"
	done
	logmsg "Packaged $ctfiles [$tsizes Kb] from directory $dir"
	let tfiles=tfiles+1
	let tdirectories=tdirectories+1
	cd $mycd
done

#########################################################################
# Do the pre, post install and remove bits, if available...		#
#########################################################################

# [[ -n "$pkg_checkinstall" ]] &&
#  print -u3 "    checkinstall          $pkg_checkinstall"
[[ -n "$pkg_preinstall" ]] &&
  print -u3 "%pre\nexec >~/preinstall-$project_name.stdout\nexec 2>~/preinstall-$project_name.stderr\n$(<$pkg_preinstall)\n"
[[ -n "$pkg_postinstall" ]] &&
  print -u3 "%post\nexec >~/postinstall-$project_name.stdout\nexec 2>~/postinstall-$project_name.stderr\n$(<$pkg_postinstall)\n"
[[ -n "$pkg_preremove" ]] &&
  print -u3 "%preun\nexec >~/preremove-$project_name.stdout\nexec 2>~/preremove-$project_name.stderr\n$(<$pkg_preremove)\n"
[[ -n "$pkg_postremove" ]] &&
  print -u3 "%postun\nexec >~/postremove-$project_name.stdout\nexec 2>~/postremove-$project_name.stderr\n$(<$pkg_postremove)\n"

#########################################################################
# We need to show the details of any prerequisites if they have been	#
# included in the configuration file.					#
#########################################################################

if [[ -n "$pkg_dependencies" ]]
then
	logmsg "Storing package dependency information..."
	firstpkg=1
	for depend_on in $(print $pkg_dependencies|tr ',' ' ')
	do
		d_pkg=$(print $depend_on|awk -v FS=: '{print $1}')
		d_minver=$(print $depend_on|awk -v FS=: '{print $2}')
		if [[ $firstpkg -eq 1 ]]
		then
			print -u3 -n "requires: $d_pkg >= $d_minver"
			firstpkg=0
		else
			print -u3 -n " $d_pkg >= $d_minver"
		fi 
	done
	print -u3 ""
fi

#########################################################################
# Now that the spec file has been built we move to the top directory	#
# of the product and attempt to build the package.			#
#########################################################################

logmsg "Building RPM package..."
cd $PROJECT_PREFIX/$project_name
rpmbuild -bb --target $pkg_arch-$pkg_vendor-linux $psf_file >$HOME/rpmbuild.$project_name.out 2>$HOME/rpmbuild.$project_name.err
r=$?
if [[ $r -eq 0 ]]
then
	file="$(awk '/Wrote:/ {print $2}' $HOME/rpmbuild.$project_name.out)"
	if [[ -n "$file" && -f "$file" ]]
	then
		cp $file $FINAL_DEPOT 2>/dev/null || {
			logwarn "Unable to move $file to $FINAL_DEPOT!";
		}
	fi

	logmsg "Package built successfully!"
	[[ -f $HOME/rpmbuild.$project_name.out ]] &&
		rm -f $HOME/rpmbuild.$project_name.out
	[[ -f $HOME/rpmbuild.$project_name.err ]] &&
		rm -f $HOME/rpmbuild.$project_name.err
	exit 0
else
	logerr "Package failed to build!\nStandard Output:\n\n"
	logerrfile $HOME/rpmbuild.$project_name.out
	logerr "\nStandard Error:\n\n"
	logerrfile $HOME/rpmbuild.$project_name.err 3
fi
exit 0
